package com.yonyou.crm.base.product.ctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.base.product.entity.PrdtypeVO;
import com.yonyou.crm.base.product.rmitf.IPrdtypeRmService;
import com.yonyou.crm.common.enumeration.entity.EnableStateEnum;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;

@RestController
@RequestMapping(value = "/base/prdtypes")
public class PrdtypeCtrl {
	
	@Autowired
	private IPrdtypeRmService service;
	
	/**
	 * 产品分类树
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "prdtypeTree", method = RequestMethod.GET)
	public Object tree(HttpServletRequest request,
			HttpServletResponse response) {
		List<Map<String, Object>> prdtypeTree = service.getTree();
		JSONObject result = new JSONObject();
		result.put("data", prdtypeTree);
		return ResponseUtil.getObject(result);
	}
	
	/**
	 * 产品分类列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public Object page(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		
		Map<String, Object> searchMap = null;
		
		if (paramObj != null) {
			
			searchMap = (Map<String, Object>)paramObj.get("searchMap");
			
			if (paramObj.containsKey(HttpKeyConstant.Page) && paramObj.containsKey(HttpKeyConstant.PageSize)) {
				return ResponseUtil.getObject(service.getPage(getRequestPage(paramObj), searchMap));
			}
		}
		
		List<PrdtypeVO> voList = service.getList(searchMap);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("voList", voList);
		
		return ResponseUtil.getObject(resultMap);
	}
	
	/**
	 * 详情
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Object detail(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		if (id == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "主键不能为空");
		}		
		
		return ResponseUtil.getObject(service.getDetail(id));
	}
	
	private Page<PrdtypeVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {
		
		Integer page = paramObj.getInt(HttpKeyConstant.Page);
		Integer pageSize = paramObj.getInt(HttpKeyConstant.PageSize);
		
		if (page == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}
		
		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}
		
		return new Page<PrdtypeVO>(page, pageSize);
	}
	
	/**
	 * 新增产品分类
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		PrdtypeVO prdType = RequestUtil.getVO(request, PrdtypeVO.class);
		
		String validMsg = validator(prdType);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
				
		return ResponseUtil.getObject(service.insert(prdType));
	}
	
	private String validator(PrdtypeVO vo) {
		
		if (vo == null) {
			return "转换实体失败";
		}

		if (StringUtils.isBlank(vo.getCode())) {
        	return  "编码不能为空";
		}
		
		if (StringUtils.isBlank(vo.getName())) {
        	return  "名称不能为空";
		}

		if (vo.getAttrGroupId() == null) {
        	return  "属性组不能为空";
		}
		
		return "";
	}
	
	/**
	 * 批量删除产品分类
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idArray = paramObj.getString("ids");		

		if (idArray == null) {
			throw new CrmMessageException("ids参数为空");
		}
		
		String[] idStrs = idArray.split(",");
		
		String message = service.batchDeleteByIDs(idStrs);
		Page<PrdtypeVO> rPage = service.getPage(getRequestPage(paramObj), (Map<String, Object>) paramObj.get("searchMap"));
						
		return ResponseUtil.getObject(rPage, PCCodeConstant.Success, message);
	}

	/**
	 * 修改产品分类
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		if (id == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "主键不能为空");
		}
		
		PrdtypeVO vo = RequestUtil.getVO(request, PrdtypeVO.class);		
		
		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		
		vo.setId(id);
								
		return ResponseUtil.getObject(service.update(vo));
		
	}
	
	/**
	 * 批量停启用
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "state", method = RequestMethod.PUT)
	public Object batchUpdateEnableState(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");		

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}
		
		String[] ids = idsStr.split(",");
		
		if (ids.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}
		
		Integer enableState = paramObj.getInt("enableState");
		
		if (enableState == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}
		
		Page<PrdtypeVO> rPage = service.batchUpdateEnableState(ids, enableState, getRequestPage(paramObj), (Map<String, Object>) paramObj.get("searchMap"));
		
		String message = (enableState == EnableStateEnum.ENABLE.getValue() ? "该分类及所有上级分类启用" : "该分类及所有下级分类停用") + "成功";
		
		return ResponseUtil.getObject(rPage, PCCodeConstant.Success, message);
	}
	
	/**
	 * 参照树
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "reftree", method = RequestMethod.GET)
	public Object getRefTree(HttpServletRequest request, HttpServletResponse response) {
		
		List<Map<String, Object>> treeList = service.getRefTree(null);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("voList", treeList);
		
		return ResponseUtil.getObject(resultMap);
	}
	
}
